package com.topideal.supplychain.hades.controller;

import com.alibaba.fastjson.JSONObject;
import com.topideal.supplychain.hades.master.model.Component;
import com.topideal.supplychain.hades.master.model.Project;
import com.topideal.supplychain.hades.master.model.User;
import com.topideal.supplychain.hades.master.service.ComponentService;
import com.topideal.supplychain.hades.master.service.ProjectService;
import com.topideal.supplychain.hades.master.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 自动补全
 *
 * @author xuxiaoyan
 * @date 2018-09-17 18:14
 */
@Controller
@RequestMapping("/autoComplete")
public class AutoCompleteController {

    private static Logger logger = Logger.getLogger(AutoCompleteController.class);

    @Autowired
    private ProjectService projectService;
    @Autowired
    private ComponentService componentService;
    @Autowired
    private UserService userService;

    /**
     * 项目名字列表
     *
     * @return
     */
    @RequestMapping(value = {"/project/{query}"})
    @ResponseBody
    public String projectDetail(@PathVariable String query) {
        List<Project> projects = null;
        JSONObject data = new JSONObject();
        try {
            projects = projectService.selectByIdOrName(query);
        } catch (Exception e) {
            logger.error("查找项目列表出错！", e);
            data.put("errorMsg", "查找项目列表出错！");
        }

        data.put("projects", projects);
        return JSONObject.toJSONString(data);
    }

    /**
     * 模块名字列表
     *
     * @return
     */
    @RequestMapping(value = {"/component/{query}"})
    @ResponseBody
    public String componentDetail(@PathVariable String query) {
        List<Component> components = null;
        JSONObject data = new JSONObject();
        try {
            components = componentService.selectByIdOrName(query);
            // 讲显示的模块名跟项目名拼接 因为有重复的模块
            if (!CollectionUtils.isEmpty(components)) {
                for (Component component : components) {
                    component.setCName("【" + component.getPName() + "】" + component.getCName());
                }
            }

        } catch (Exception e) {
            logger.error("查找模块列表出错！", e);
            data.put("errorMsg", "查找模块列表出错！");
        }

        data.put("components", components);
        return JSONObject.toJSONString(data);
    }

    /**
     * 姓名
     *
     * @return
     */
    @RequestMapping(value = {"/userkey/{query}"})
    @ResponseBody
    public String userkeyDetail(@PathVariable String query) {
        List<User> users = null;
        JSONObject data = new JSONObject();
        try {
            users = userService.selectByQuery(query);
        } catch (Exception e) {
            logger.error("查找姓名列表出错！", e);
            data.put("errorMsg", "查找姓名列表出错！");
        }

        data.put("users", users);
        return JSONObject.toJSONString(data);
    }
}
